<?php

require_once("View/View/LoggedInView.php");
require_once("Database/Model/ExerciseDAL.php");
require_once("Database/Model/MuscleGroupDAL.php");
require_once("Exercises/Model/ExerciseModel.php");

class ExersicesView
{
	/**
	*@var ExerciseDAL
    **/	
	private $exerciseDAL;
	/**
	*@var LoggedInView
    **/
	private $loggedInView;
	/**
	*@var ExerciseModel
    **/
	private $exerciseModel;
	/**
	*@var MuscleGroupDAL
    **/
	private $muscleGroupDAL;
	/**
	*@var string sessionLocation
    **/
	private static $edit = "edit";
	/**
	*@var string sessionLocation
    **/
	private static $remove = "remove";
	/**
	*@var string sessionLocation
    **/
	private static $edited = "edited"; 
	/**
	*@var string sessionLocation
    **/
	private static $added = "added";
	/**
	*@var string sessionLocation
    **/
	private static $new = "new";
	/**
	*@var string getLocation
    **/
	private static $finishedEdit = "ExerciseView::finishedEdit";
	/**
	*@var int getLocation
    **/	
	private static $idToEdit = "ExerciseView::IdToEdit";
	/**
	*@var string getLocation
    **/
	private static $idToRemove = "ExerciseView::IdToRemove";
	/**
	*@var int getLocation
    **/
	private static $createdExerciseID = "ExerciseView::createdExerciseID";
	/**
	*@var string getLocation
    **/
	private static $newMuscleGroup = "ExerciseView::newMouscleGroup";
	/**
	*@var string getLocation
    **/
	private static $newExerciseName = "ExerciseView::newExerciseName";
	/**
	*@var string getLocation
    **/
	private static $newExerciseDescription = "ExerciseView::newExerciseDescription";
	/**
	*@var int getLocation
    **/
	private static $editID = "ExerciseView::editID";
	/**
	*@var string getLocation
    **/
	private static $editMuscleGroup = "ExerciseView::editMouscleGroup";
	/**
	*@var string getLocation
    **/
	private static $editExerciseName = "ExerciseView::editExerciseName";
	/**
	*@var string getLocation
    **/
	private static $editExerciseDescription = "ExerciseView::editExerciseDescription";
	/**
	*@var array() strings
    **/
	private $muscleGroupNames = array(1 =>"Översida lår",2=> "Undersida lår",3=> "Vader",4=> "Biceps",
									   5=> "Triceps", 6=> "Axlar", 7=> "Bröst",8=> "Mage",9=> "Handleder",
									   10=>"Nedre rygg",11=> "Övre rygg");


	public function __construct()
	{
		$this->exerciseDAL = new ExerciseDAL();
		$this->loggedInView = new LoggedInView();
		$this->exerciseModel = new ExerciseModel();
		$this->muscleGroupDAL = new MuscleGroupDAL();
	}

	/**
	*@return string html
    **/
	public function getExercisesPage()
	{
		$html = $this->getExercisesPageHeader();
		$html .= $this->getExercisesPageBody();
		$html .= $this->getExercisesPageFooter();
		return $html;
	}

	/**
	*@return string html
    **/
	private function getExercisesPageHeader()
	{
		$header = $this->loggedInView->getLoggedInHeader();
		return $header;
	}

	/**
	*@return string html
    **/
	private function getExercisesPageBody()
	{
		$html = "<div id='ExercisesMenuDiv'>".$this->getMenu();                    
		$html .= "<li>
						<a href='Exercises.php?new'>Lägg till ny övning<a/>
			      </li>
				</div>";

		$html .= $this->checkWhatPage();
				  
		return $html;
	}

	/**
	*@return string html
    **/
	private function checkWhatPage()
	{
		if($this->checkIfCreateNewExercise() === true)
	    {
	    	$html = $this->createNewExerciseForm();
	    }
	    elseif($this->editExerciseOnID() === true)
		{

			$html = $this->editExerciseForm();
		}	
		else
		{	
			if(($this->exerciseModel->getErrorMessage() != "" || $this->exerciseModel->getSuccesMessage() == "") 
				&& $this->checkIfAddNewExercise())
			{
				$html = $this->createNewExerciseForm();
			}
			elseif(($this->exerciseModel->getErrorMessage() != "" || $this->exerciseModel->getSuccesMessage() == "") 
				&& $this->editedExercise())
			{
				$html = $this->editExerciseForm();
			}		       
			else
			{
			$html = $this->getExercises($this->exerciseModel->getSuccesMessage(), 
									    $this->exerciseDAL->getExerciseList(),
									    true);
			}
		}
		return $html;
	}


	/**
	*@return string html
    **/
	private function editExerciseForm() 
	{	
		$message;
		$errormessage = $this->exerciseModel->getErrorMessage();
		$successMessage = $this->exerciseModel->getsuccesMessage();

		if($errormessage != "")
		{
			$message = "<div id='errorMessage'><p>$errormessage</p></div>";
		}
		elseif($successMessage != "")
		{
			$message = "<div id='successMessage'><p>$successMessage</p></div>";
		}

		$id = $_GET[self::$edit];
		if($id == null)
		{
			$id = $this->exerciseModel->getIdToEdit();
		}
		$exercise = $this->exerciseDAL->getExerciseList($id);
		$eName = $exercise->getExerciseName();
		$description = $exercise->getExerciseDescription();
	

		$html = "
		<div id='registerFormDiv' >
			<a href='Exercises.php'>tillbaka</a>
			<form action='?edited' method='post' enctype='multipart/form-data'>
				<fieldset> 
					<legend>Lägg till ny övning</legend>
					$message
						<label for='mouscleGroup' >Muskelgrupp :</label>
						<select name='" . self::$editMuscleGroup . "' id='mouscleGroup'>";

		$arr = $this->muscleGroupDAL->getMuscleGroupList();

		foreach($arr as $value) 
		{
			$id = $value->getMuscleGroupID();
			$name = $value->getMuscleGroup();
			$html .= "<option value='$id'>$name</option>";
		}

		$html .= "</select>
					<label for='exerciseName' >Övningsnamn :</label>
					<input type='text' size='40' name='" . self::$editExerciseName . "' id='exerciseName' value='$eName' />
						
					<label for='exerciseDescription' >Beskrivning :</label>
					<textarea rows='4' cols='50' name='" . self::$editExerciseDescription . "' id='exerciseDescription'>$description</textarea>

					<input type='submit' name=''  value='Redigera' />
				</fieldset>
			</form>
		</div>";

		return $html;
	}

	/**
	*@return string html
    **/
	private function createNewExerciseForm()
	{
		$message;
		$errormessage = $this->exerciseModel->getErrorMessage();
		$successMessage = $this->exerciseModel->getsuccesMessage();

		if($errormessage != "")
		{
			$message = "<div id='errorMessage'><p>$errormessage</p></div>";
		}
		elseif($successMessage != "")
		{
			$message = "<div id='successMessage'><p>$successMessage</p></div>";
		}

		$html = "
		<div id='registerFormDiv' >
			<a href='Exercises.php'>tillbaka</a>
			<form action='?added' method='post' enctype='multipart/form-data'>
				<fieldset> 
					<legend>Lägg till ny övning</legend>
						$message
						<label for='mouscleGroup' >Muskelgrupp :</label>
						<select name='" . self::$newMuscleGroup . "' id='mouscleGroup'>";

		$arr = $this->muscleGroupDAL->getMuscleGroupList();

		foreach($arr as $value) 
		{
			$id = $value->getMuscleGroupID();
			$name = $value->getMuscleGroup();
			$html .= "<option value='$id'>$name</option>";
		}

		$html .= "</select>
					<label for='exerciseName' >Övningsnamn :</label>
					<input type='text' size='40' name='" . self::$newExerciseName . "' id='exerciseName' value='' />
						
					<label for='exerciseDescription' >Beskrivning :</label>
					<textarea rows='4' cols='50' name='" . self::$newExerciseDescription . "' id='exerciseDescription'></textarea>

					<input type='submit' name=''  value='Lägg till' />
				</fieldset>
			</form>
		</div>";

		return $html;

	}

	/**
	*@param string html
	*@param ExerciseListhtml
	*@param boolean
	*@return string html
    **/
	public function getExercises($message = null, $exerciseList, $onlyView = false)
	{
		if($message == null)
		{
			$message = "Övningar";
		}

		$html = "<div id='exercisesViewexercisesDiv'><div id='ExercisesViewMessage'><p>$message</p></div>";

		$upperThigh = "<div id='oneExercise'><h2>Översida lår</h2>"; $lowerThigh= "<div id='oneExercise'><h2>Undersida lår</h2>"; 
		$calves= "<div id='oneExercise'><h2>Vader</h3>"; $biceps= "<div id='oneExercise'><h2>Biceps</h2>"; 
		$triceps= "<div id='oneExercise'><h2>Triceps</h2>"; $shoulders= "<div id='oneExercise'><h2>Axlar</h2>";
		$chest= "<div id='oneExercise'><h2>Bröst</h2>"; $abs = "<div id='oneExercise'><h2>Mage</h2>";	
	    $wrists= "<div id='oneExercise'><h2>Handleder</h2>"; $lowerBack= "<div id='oneExercise'><h2>Nedre rygg</h2>"; 
	    $upperBack= "<div id='oneExercise'><h2>Övre rygg</h2>";
		$displayMuscle = array(1 => false, 2 => false, 3 => false, 4 => false, 5 => false, 6 => false, 7 => false , 8 => false, 9 => false, 10 => false, 11 => false);
		foreach($exerciseList as $oneExercise)
		{
			$id = $oneExercise->getExerciseID();
	    	$musclegroup = $oneExercise->getMusclegroup();
		    $name = $oneExercise->getExerciseName();
			$description = $oneExercise->getExerciseDescription();
			//<p>$musclegroup</p>
			$htmls  = "
						<h3>$name</h3>
						<p>$description</p>";
			if($onlyView)
			{
				$htmls .= "<a href='?remove=$id'>Ta bort</a> <a href='?edit=$id' id='exerciseAtag'>Redigera</a>";
			}

		    switch($musclegroup = $oneExercise->getMusclegroupID())
		    {
			    case 1:
			    	$upperThigh .=$htmls; 
			    	$displayMuscle[1] = true; break;
			    case 2:
			    	$lowerThigh .=$htmls; 
			    	$displayMuscle[2] = true; break;
			    case 3:
			    	$calves .=$htmls;
			    	$displayMuscle[3] = true; break;
			    case 4:
			    	$biceps .=$htmls;
			    	$displayMuscle[4] = true; break;
			    case 5:
			    	$triceps .=$htmls;
			    	$displayMuscle[5] = true; break;
			    case 6: 
			    	$shoulders .=$htmls;
			    	$displayMuscle[6] = true; break;
			    case 7:
			    	$chest .=$htmls;
			    	$displayMuscle[7] = true; break;
			    case 8:
			    	$abs .=$htmls;
			    	$displayMuscle[8] = true; break;
			    case 9:
			    	$wrists .=$htmls;
			    	$displayMuscle[9] = true; break;
			    case 10:
			    	$lowerBack .=$htmls;
			    	$displayMuscle[10] = true; break;
			    case 11:
			    	$upperBack .=$htmls;
			    	$displayMuscle[11] = true; break;
		    }
		}

		$upperThigh .= "</div>"; $lowerThigh .= "</div>"; 
		$calves .= "</div>"; $biceps .= "</div>"; 
		$triceps .= "</div>"; $shoulders .= "</div>";
		$chest .= "</div>"; $abs .= "</div>";
	    $wrists .= "</div>"; $lowerBack .= "</div>"; 
	    $upperBack .= "";
		
	    if($displayMuscle[1])
	    {
	    	$html .= $upperThigh;
	    }
	     if($displayMuscle[2])
	    {
	    	$html .= $lowerThigh;
	    }
	     if($displayMuscle[3])
	    {
	    	$html .= $calves;
	    }
	     if($displayMuscle[4])
	    {
	    	$html .= $biceps;
	    }
	     if($displayMuscle[5])
	    {
	    	$html .= $triceps;
	    }
	     if($displayMuscle[6])
	    {
	    	$html .= $shoulders;
	    }
	     if($displayMuscle[7])
	    {
	    	$html .= $chest;
	    }
	     if($displayMuscle[8])
	    {
	    	$html .= $abs;
	    }
	     if($displayMuscle[9])
	    {
	    	$html .= $wrists;
	    }
	     if($displayMuscle[10])
	    {
	    	$html .= $lowerBack;
	    }
	     if($displayMuscle[11])
	    {
	    	$html .= $upperBack;
	    }
	    $html .= "</div>";
		return $html;
	}

	/**
	*@return string html
    **/
	private function getMenu()
	{
		$html = $this->loggedInView->getLoggedInMenu();
	    return $html;
	}

	/**
	*@return string html
    **/
	private function getExercisesPageFooter()
	{
		$footer = $this->loggedInView->getFooter();
		return $footer;
	}



// GET //  GET // GET // GET // GET // GET //  GET // GET // GET // GET // GET //  GET // GET // GET // GET // GET //  GET // GET // GET // GET // GET //  GET // GET // GET // GET 

	/**
	*@return boolean
    **/
	public function finishedEdit()
	{
		if(isset($_GET['edited']))
		{
			return true;
		}
		return false;
	}

	/**
	*@return int
    **/
	public function getEditID()
	{
		if(isset($_SESSION[self::$editID]))
		{
			return $_SESSION[self::$editID];
		}
		return 0;
	}

	/**
	*@return int
    **/
	public function getIdToEdit()
	{
		if(isset($_GET[self::$edit]))
		{
			return $_GET[self::$edit];
		}
		return 0;
	}

	/**
	*@return int
    **/
	public function getIdToRemove()
	{
		if(isset($_GET[self::$remove]))
		{
			return $_GET[self::$remove];
		}
		return 0;
	}

	/**
	*@return int
    **/
	public function getcreatedExerciseID()
	{
		if(isset($_GET[self::$createdExerciseID]))
		{
			return $_GET[self::$createdExerciseID];
		}
		return 0;
	}

	/**
	*@return boolean
    **/
	public function removeExerciseOnID()
	{
		if(isset($_GET[self::$remove]) && $_GET[self::$remove] != null)
		{
			return true;
		}
		return false;
	}


	/**
	*@return boolean
    **/
	public function editExerciseOnID()
	{
		if(isset($_GET[self::$edit]) && $_GET[self::$edit] != null)
		{
			return true;
		}
		return false;
	}

	/**
	*@return boolean
    **/
	public function editedExercise()
	{
		if(isset($_GET[self::$edited]))
		{
			return true;
		}
		return false;
	}

	/**
	*@return boolean
    **/
	public function checkIfCreateNewExercise()
	{
		if(isset($_GET[self::$new]))
		{
			return true;
		}
		return false;
	}

	/**
	*@return boolean
    **/
	public function checkIfAddNewExercise()
	{
		
		if(isset($_GET[self::$added]))
		{
			return true;
		}
		return false;
	}




// NEW 			NEW 			NEW 				NEW 				NEW 				NEW 			NEW 

	/**
	*@return POST int
    **/
	public function getNewMuscleGroupID()
	{
		if(isset($_POST[self::$newMuscleGroup]))
		{
			return $_POST[self::$newMuscleGroup];
		}
		return "";
	}

	/**
	*@return POST string
    **/
	public function getNewExerciseName()
	{
		if(isset($_POST[self::$newExerciseName]))
		{
			return $_POST[self::$newExerciseName];
		}
		return "";
	}

	/**
	*@return POST string
    **/
	public function getNewExerciseDescription()
	{
		if(isset($_POST[self::$newExerciseDescription]))
		{
			return $_POST[self::$newExerciseDescription];
		}
		return "";
	}



// EDIT              EDIT                 EDIT                 EDIT                EDIT                  EDIT  
	
	/**
	*@return POST int
    **/
	public function getEditMuscleGroupID()
	{
		if(isset($_POST[self::$editMuscleGroup ]))
		{
			return $_POST[self::$editMuscleGroup ];
		}
		return "";
	}
	/**
	*@return POST string
    **/
	public function getEditExerciseName()
	{
		if(isset($_POST[self::$editExerciseName]))
		{
			return $_POST[self::$editExerciseName];
		}
		return "";
	}

	/**
	*@return POST string
    **/
	public function getEditExerciseDescription()
	{
		if(isset($_POST[self::$editExerciseDescription]))
		{
			return $_POST[self::$editExerciseDescription];
		}
		return "";
	}
	
 	 

}